import http from "../../utils/http.js";
import tools from "../../utils/tools.js";
import storage from "../../utils/storage.js";

export default {
	state: {
		$bus: null,
		inited: false,
		info: {},
		menus: [],
		path: '',
	},
	mutations: {
	},
	actions: {
		async validateLogin({ state, dispatch }) {
			if (state.inited) return;
			const token = storage.getVoucher();
			if (!token) return tools.toLogin();
			const res = await http.get('/author');
			if (res && res.code === 200) {
				setTimeout(() => {
					dispatch('initState', res.data);
				}, 500)
				return;
			}
		},

		logined({ state, dispatch }, resp) {
			dispatch('initState', resp);
			storage.setVoucher(resp.authorization);
		},

		initState({ state, dispatch }, data) {
			const menus = data.menus;
			delete data.menus;
			state.info = data
			state.menus = menus
			state.inited = true;
		},

		logout({ state, dispatch }) {
			state.info = {}
			state.menus = []
			state.inited = false
			// 清除本地存储数据
			storage.delVoucher();
			tools.toLogin();
		},
	}
}
